package com.sc.nft.dao;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sc.nft.entity.dto.SaasTaskAwardDTO;
import com.sc.nft.entity.dto.SaasTaskStageConfigurationListVO;
import com.sc.nft.mapper.SaasTaskStageConditionsMapper;
import com.sc.nft.entity.SaasTaskStageConditions;
import com.sc.nft.service.SaasTaskStageConditionsService;
import jodd.util.CollectionUtil;
import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 任务阶段达成条件表(SaasTaskStageConditions)表Dao层
 *
 * @author 王伟成
 * @since 2024-04-12 18:35:26
 */
@Slf4j
@Repository
public class SaasTaskStageConditionsDao extends ServiceImpl<SaasTaskStageConditionsMapper, SaasTaskStageConditions> {

    public SaasTaskStageConditions upAndDownSection(Long taskId,Integer offTheStocks) {
        LambdaQueryWrapper<SaasTaskStageConditions> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.gt(SaasTaskStageConditions::getUpperLimit, offTheStocks);
        queryWrapper.le(SaasTaskStageConditions::getLowerLimit, offTheStocks);
        queryWrapper.eq(SaasTaskStageConditions::getSaasTaskId, taskId);
        queryWrapper.gt(SaasTaskStageConditions::getRemainQuota, 0);
        queryWrapper.last("limit 1");
        return this.getOne(queryWrapper);
    }
    public Boolean removeByTaskId(Long taskId) {
        LambdaQueryWrapper<SaasTaskStageConditions> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SaasTaskStageConditions::getSaasTaskId, taskId);
        boolean remove = this.remove(queryWrapper);
        return remove;
    }
    /**
     * 根据任务获取阶段配置列表
     * @param taskId
     * @return
     */
    public List<SaasTaskStageConfigurationListVO> stageConfigurationList(Long taskId) {
        LambdaQueryWrapper<SaasTaskStageConditions> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SaasTaskStageConditions::getSaasTaskId, taskId);
        List<SaasTaskStageConditions> list = this.list(queryWrapper);
        return CollUtil.isNotEmpty(list) ? BeanUtil.copyToList(list, SaasTaskStageConfigurationListVO.class) : null;
    }
    public List<SaasTaskStageConditions> getTaskId(Long taskId) {
        LambdaQueryWrapper<SaasTaskStageConditions> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SaasTaskStageConditions::getSaasTaskId, taskId);
        return this.list(queryWrapper);
    }
    /**
     * 根据任务id,获取他当前最近的一个阶段
     * @param taskId
     * @return
     */
    public SaasTaskStageConditions getByTaskIdStageConfig(Long taskId) {
        return baseMapper.getByTaskIdStageConfig(taskId);
    }
    public Boolean minusInventory(Long id, Integer num) {
        LambdaUpdateWrapper<SaasTaskStageConditions> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(SaasTaskStageConditions::getId, id);
        updateWrapper.gt(SaasTaskStageConditions::getRemainQuota, 0);
        updateWrapper.setSql("remain_quota = remain_quota - " + num);
        return this.update(updateWrapper);
    }
    /**
     * 根据任务id获取最大的上限
     * @param taskId
     * @return
     */
    public Integer getTaskIdMaxUpperLimit(Long taskId) {
        return baseMapper.getTaskIdMaxUpperLimit(taskId);
    }
}
